package com.lagou.server;


import com.lagou.model.Exam;
import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.util.Iterator;
import java.util.List;

public class ServerDao {

    private List<User> users;
    private List<Exam> exams;

    public ServerDao(List<User> users, List<Exam> exams) {
        this.users = users;
        this.exams = exams;
    }

    /**
     * 编程实现管理员账号和密码的校验并将结果返回出去
     */
    public boolean serverManagerCheck(User user) {
        if ("admin".equals(user.getUserName()) && "123456".equals(user.getPassword())) {
            return true;
        }
        return false;
    }


    /**
     * 实现服务器下线前的数据存储
     */
    public void serverQuit(){
        ServerFileList sfl = new ServerFileList(users,exams);
        sfl.listToFile();
    }

    /**
     *实现增加学生信息
     */
    public boolean serverAddStudent(User user){
        for (User u:users){
            //查重，判断之前集合中是否有重名
            if (u.getUserName().equals(user.getUserName())){
                return false;
            }}
        users.add(user);
        return true;
    }


    /**
     *实现删除学生信息
     */

    public boolean serverDeleteStudent(User user){
        Iterator iterator1 = users.iterator();
        while(iterator1.hasNext()){
            Object obj = iterator1.next();
            String name = ((User) obj).getUserName();
            if (user.getUserName().equals(name)){
                iterator1.remove();
                return true;
            }
        }
        return false;
    }


    /**
     *实现修改学生信息
     */

    public boolean serverModifyStudent(User user){
        for (User u:users){
            if (u.getUserName().equals(user.getUserName())){
                u.setPassword(user.getPassword());
                return true;
            }
        }
        return false;
    }
    /**
     *实现查询学生信息
     */
    public User serverStudentQuery(User user){
        for (User u:users){
            if (u.getUserName().equals(user.getUserName())){
                return u;
            }
        }
        return null;
    }
    /**
     *实现增加考题
     */
    public boolean serverAddExamQuestions(Exam exam){
        for (Exam e:exams){
            if (e.getTitle().equals(exam.getTitle())){
                return false;
            }
        }
        exams.add(exam);
        return true;
    }
    /**
     *实现删除考题
     */
    public boolean serverDeleteExamQuestions(Exam exam){
        Iterator iterator1 = exams.iterator();
        while(iterator1.hasNext()){
            Object obj = iterator1.next();
            String title = ((Exam) obj).getTitle();
            if (exam.getTitle().equals(title)){
                iterator1.remove();
                return true;
            }
        }
        return false;
    }

    /**
     *实现修改考题
     */
    public boolean serverModifyExamQuestions(Exam exam){
        for (Exam e:exams){
            if (e.getTitle().equals(exam.getTitle())){
                e.setA(exam.getA());
                e.setB(exam.getB());
                e.setC(exam.getC());
                e.setD(exam.getD());
                e.setAnswer(exam.getAnswer());
                return true;
            }
        }
        return false;
    }
    /**
     *实现查询考题
     */
    public Exam serverQueryExamQuestions(Exam exam){
        for (Exam e:exams){
            if (e.getTitle().equals(exam.getTitle())&&exams.size()!=0){
                return e;
            }
        }
        return null;
    }


    /**
     *实现校验学生信息
     */
    public boolean serverCheckStudent(User user){
        for (User u:users){
            if (u.getUserName().equals(user.getUserName())&&u.getPassword().equals(user.getPassword())){
                return true;
            }
        }
        return false;
    }
    /**
     *实现修改密码
     */
    public boolean serverModifyPassword(User user){
        for (User u:users){
            if (u.getUserName().equals(user.getUserName())){
                u.setPassword(user.getPassword());
                return true;
            }
        }
        return false;
    }
    /**
     *实现考试题目的传递
     */
    public List<Exam> serverStartExam(){
        if (exams.size() != 0) {
            return exams;
        }
        return null;
    }
    /**
     *实现提交考题，计算分数
     */
    public boolean serverSubmitExam(UserMessage um) {
        int count = 0;
        List<Exam> exams1 = um.getExams();
        int altogether = exams.size();
        for (int i = 0; i < altogether; i++) {
            if (exams1.get(i).getAnswer().equals(exams.get(i).getAnswer())) {
                count++;
            }
        }
        for (User u : users) {
            if (u.getUserName().equals(um.getUser().getUserName())) {
                u.setScore(count * 1.0 / altogether * 100);
                u.getScores().add(u.getScore());
                um.setUser(u);
                return true;
            }
        }
        return false;
    }
    /**
     *实现查询分数
     */
    public User serverQueryScore(User user){
        for (User u:users){
            if (u.getUserName().equals(user.getUserName())){
                user.setScores(u.getScores());
                return user;
            }
        }
        return null;
    }
}